/**
 * 活动搜索servlet
 */
package org.morningangel.servlet.android;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.morningangel.android.bean.SearchActivityList;
import org.morningangel.bean.Activity;
import org.morningangel.dao.ActivityDao;
import org.morningangel.dao.impl.ActivityDaoImpl;

@WebServlet(name = "AndroidSearchActivityServlet", urlPatterns = "/android/searchActivity")
public class SearchActivityServlet extends HttpServlet {

	private static final long serialVersionUID = -1259889611836939568L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String searchContent = request.getParameter("searchContent");
		String maxId = request.getParameter("maxId");
		ActivityDao activityDao = new ActivityDaoImpl();
//		List<Activity> list = activityDao.search(searchContent);
		List<Activity> list =null;
		if(maxId.equals("0")){//若为请求刷新或者初次请求搜索，则默认选择最新10条记录
			list= activityDao.listNewPage(Integer.parseInt("10"),searchContent);
		}else{
			list= activityDao.listSearchPage(Integer.parseInt(maxId),Integer.parseInt("10"),searchContent);
		}
		
		SearchActivityList result = null;
		if (list.size() == 0) {
			result = new SearchActivityList();
		} else {
			result = new SearchActivityList(list);
		}
		JSONObject jsonObject = JSONObject.fromObject(result);
		PrintWriter out = response.getWriter();
		out.print(jsonObject.toString());
		out.flush();
		out.close();
	}

}
